//
//  CFNetworkFromAFNetworking.m
//  网络封装(Xcode7)
//
//  Created by dllo on 15/9/30.
//  Copyright © 2015年 Fan. All rights reserved.
//

#import "CFNetworkFromAFNetworking.h"
#import "AFNetworking.h"
#import "ChangeString.h"
@implementation CFNetworkFromAFNetworking
+ (void)askObjectByURLString:(NSString *)string
                  BodyString:(NSString *)bodyString
             WithObjectBlock:(void(^)(id object))objectBlock;
{
    string = [string stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet characterSetWithCharactersInString:string]];
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html", @"application/javascript", nil];
    if (!bodyString) {
        [manager GET:string parameters:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
            objectBlock(responseObject);
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"没有网哦" message:nil delegate:self cancelButtonTitle:nil otherButtonTitles:@"确认", nil];
            [alertView show];
            ////NSLog(@"请求失败😢");
        }];
        
    }else{
        bodyString = [bodyString stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet characterSetWithCharactersInString:bodyString]];
        [manager POST:string parameters:[ChangeString getDictionaryByString:bodyString] success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
            objectBlock(responseObject);
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"没有网哦" message:nil delegate:self cancelButtonTitle:nil otherButtonTitles:@"确认", nil];
            [alertView show];
            ////NSLog(@"请求失败😢");
        }];
        
    }
}
@end
